//
//  YWMonitorAlarmTableViewController.m
//  maintain
//
//  Created by kyo on 16/12/8.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWMonitorAlarmTableViewController.h"
#import "YWMacro.h"
#import "UIColor+CustomColors.h"
#import "UIScrollView+EmptyDataSet.h"
#import "UITableView+FDTemplateLayoutCell.h"
#import "MJRefresh.h"
#import "YWCommonTool.h"
#import "NSDate+YYAdd.h"
#import "YWEwinManager.h"

#import "YWMonitorAlarmTableViewCell.h"
#import "YWMonitorAlarmModel.h"
#import "YWMonitorAlarmManager.h"

@interface YWMonitorAlarmTableViewController ()<DZNEmptyDataSetDelegate, DZNEmptyDataSetSource>
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) NSMutableArray *dataSource;
@end

@implementation YWMonitorAlarmTableViewController

- (instancetype)init{
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = NSLocalizedString(@"monitorAlarm", nil);
    self.page = 1;
    [self setupTableView];
    
//    UIAlertController* alert= [UIAlertController alertControllerWithTitle:@"提示" message:@"一切正常!" preferredStyle:UIAlertControllerStyleAlert];
//    [alert addAction:[UIAlertAction actionWithTitle:@"好" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        [self.navigationController popViewControllerAnimated:YES];
//    }]];
//    [self presentViewController:alert animated:YES completion:nil];
}

- (void)setupTableView{
    self.view.backgroundColor = [UIColor customBackgroudColor];
    [self.tableView registerClass:[YWMonitorAlarmTableViewCell class] forCellReuseIdentifier:YWMonitorAlarmTableViewCellIdentifier];
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.sectionFooterHeight = 0.01;
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.emptyDataSetSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.estimatedRowHeight = 44.0f;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshTop)];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshFootor)];
    self.tableView.mj_footer.automaticallyHidden = YES;
    // 马上进入刷新状态
    [self.tableView.mj_header beginRefreshing];
}

- (void)refreshTop{
    self.page = 1;
    [self.tableView.mj_footer resetNoMoreData];
    [self requestData:YWTableViewFreshTypeDown];
}

- (void)refreshFootor{
    [self requestData:YWTableViewFreshTypeUp];
}

- (void)requestData:(YWTableViewFreshType)refreshType{
    if (refreshType == YWTableViewFreshTypeUp) {
        self.page++;
    }
    //请求数据刷新
    YWWeakSelf(weakSelf);
    [[YWMonitorAlarmManager shareManager] requestMonitorAlarmWithPage:self.page pageSize:10 WithComplete:^(BOOL success) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (refreshType == YWTableViewFreshTypeDown) {
                [weakSelf.tableView.mj_header endRefreshing];
            }else {
                [weakSelf.tableView.mj_footer endRefreshing];
            }
            if (success) {
                weakSelf.dataSource = nil;
                [weakSelf.tableView reloadData];
                //上次阅读时间
                NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970]*1000;
                [[YWEwinManager shareManager] saveLastReadTime:currentTime type:YWUnReadCountTypeMonitorAlarm];
            }
        });
    }];
}

- (NSMutableArray*)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
        [_dataSource addObjectsFromArray:
        [[YWMonitorAlarmManager shareManager] monitorAlarmArrayWithPage:self.page]];
    }
    return _dataSource;
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

#pragma mark - Table view dalegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YWMonitorAlarmTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:YWMonitorAlarmTableViewCellIdentifier];
    cell.monitorAlarm = self.dataSource[indexPath.section];
    return cell;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    return [tableView fd_heightForCellWithIdentifier:YWMonitorAlarmTableViewCellIdentifier cacheByIndexPath:indexPath configuration:^(YWMonitorAlarmTableViewCell *cell) {
//        cell.fd_enforceFrameLayout = NO;
//        cell.monitorAlarm = self.dataSource[indexPath.section];
//    }];
//}

- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section{
    return 35;
}

#pragma mark - table view section header view
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    static NSString* HeaderIdentifier = @"YWMonitorAlarmTableViewControllerForHeaderInSection";
    UITableViewHeaderFooterView* headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HeaderIdentifier];
    if (headerView == nil) {
        headerView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:HeaderIdentifier];
        headerView.contentView.backgroundColor = [UIColor customBackgroudColor];
        UILabel* label = [[UILabel alloc] init];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor blackColor];
        label.tag = 2016;
        label.font = KTitleFont;
        [headerView addSubview:label];
    }
    
    NSString *headText = [self titleForHeaderInSection:section];
    UILabel* label = [headerView viewWithTag:2016];
    label.text = headText;
    label.textColor = [UIColor customTextGrayBlackColor];
    label.frame = CGRectMake(15, 0, SCREEN_WIDTH - 2 * 15, 35);
    return headerView;
}

- (NSString *)titleForHeaderInSection:(NSInteger)section{
    YWMonitorAlarmModel * monitor = self.dataSource[section];
    NSString * timeString = [YWCommonTool showTime:monitor.createTime/1000 andFormatter:KTimeFormatShortTime];
    return timeString;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
